package cn.doitedu.dashboard;

import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author: 深似海
 * @Site: <a href="www.51doit.com">多易教育</a>
 * @QQ: 657270652
 * @Date: 2024/5/6
 * @Desc: 学大数据，上多易教育
 *   每5分钟，各业务线的  pv、uv、会话数
 **/
public class Job01_5MPVUV {

    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(5000, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointStorage("file:///D:/flink/ck");

        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);

        // 建表，映射 dwd层的用户行为明细宽表
        tenv.executeSql(
                "create table dwd_events_kafka(     " +
                        "     session_id         string             " +
                        "    ,event_id           string             " +
                        "    ,action_time        bigint             " +
                        "    ,user_id bigint           " +
                        "    ,page_service    string   " +
                        "    ,rt as to_timestamp_ltz(action_time,3)   " +
                        "    ,watermark for rt as rt - interval '0' second   " +

                        ") WITH (                      " +
                        "    'connector' = 'kafka',    " +
                        "    'topic' = 'dwd-user-action-log',  " +
                        "    'properties.bootstrap.servers' = 'doitedu:9092',  " +
                        "    'properties.group.id' = 'doit44_g1',              " +
                        "    'scan.startup.mode' = 'latest-offset',            " +
                        "    'value.format' = 'json',                          " +
                        "    'value.fields-include' = 'EXCEPT_KEY'             " +
                        ")                                                     ");


        // 建表，映射mysql中的目标表
        tenv.executeSql(
                " create table sink_mysql(                                 "+
                        "     page_service  string,                        "+
                        "     window_start timestamp(3),                   "+
                        "     window_end timestamp(3),                     "+
                        "     pv bigint,                                   "+
                        "     uv bigint,                                   "+
                        "     ses_cnt bigint,                                "+
                        "     primary key(page_service) not enforced         "+

                        " ) with (                                          "+
                        "  'connector' = 'jdbc',                            "+
                        "  'url' = 'jdbc:mysql://doitedu:3306/doit47',      "+
                        "  'table-name' = 'dashboard_m5_pv_uv',             "+
                        "  'username' = 'root',                             "+
                        "  'password' = 'root'                              "+
                        " )                                                 "
        );



        tenv.executeSql(
                        "insert into sink_mysql  " +
                        "select                                                                       "+
                        "     page_service,                                                            "+
                        "     window_start,                                                            "+
                        "     window_end,                                                              "+
                        "     count(event_id) filter(where event_id = 'page_load' ) as pv,             "+
                        "     count(distinct user_id) as uv,                                           "+
                        "     count(distinct session_id) as ses_cnt                                    "+
                        " from TABLE(                                                                  "+
                        "     tumble(table dwd_events_kafka,descriptor(rt),interval '5' minute )  "+
                        " )                                                                            "+
                        " group by                                                                     "+
                        "     window_start,                                                            "+
                        "     window_end,                                                              "+
                        "     page_service                                                             "
        );



    }

}
